package com.aaa.javaweb.servlet;

import com.aaa.javaweb.dao.TChukuDao;
import com.aaa.javaweb.dao.TPandianDao;
import com.aaa.javaweb.entity.SheShi;
import com.aaa.javaweb.entity.TChuku;
import com.aaa.javaweb.entity.TPandian;
import com.aaa.javaweb.entity.TPandiandetail;
import com.aaa.javaweb.until.PageInfo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class TPandiandetailController {
    @Autowired
    private TPandianDao pandianDao;

    @RequestMapping("/getSheShiPd")
    public PageInfo<TPandiandetail> getSheShiPd(TPandian pandian) {
        PageInfo<TPandiandetail> info = new PageInfo<>();
        long count = pandianDao.getSheShiCount();
        if (count > 0) {
            info.setCount(count);
            List<TPandiandetail> allRuku = pandianDao.getAllSheShi(pandian);
            info.setData(allRuku);
        }
        return info;
    }
    @RequestMapping("/pd")
    public String ruku(HttpServletRequest request) {
        Map map = (Map) JSON.parse(request.getParameter("data"));
        // 将接收的入库表格数据 转化成 List<Map> 用于添加入库详情单 和 让图书数量增加
        List<Map> tableData = (List<Map>) JSON.parse(request.getParameter("tableData"));
        int rukuid = pandianDao.addPandian(map);
        if (rukuid > 0) {
            // 添加入库详情表
            int result = pandianDao.addPandianDetail(tableData, rukuid);
            if (result > 0) {
                // 增加商品的数量,返回影响的总行数
                int i = pandianDao.pandianAndAddBookCount(tableData);
                return i + "";
            }
        }
        return "0";
    }
    @RequestMapping("/getAllPd")
    public PageInfo<TPandian> getAllRuku(TPandian pandian){
        PageInfo<TPandian> info = new PageInfo<>();
        long count = pandianDao.getCount(pandian);
        if (count > 0) {
            info.setCount(count);
            List<TPandian> allRuku = pandianDao.getAllRuku(pandian);
            info.setData(allRuku);
        }
        return info;
    }
    @RequestMapping("/getAllPdDetailByCkId")
    public PageInfo<Map> getAllRukuDetailByRukuId(String pdid) {
        PageInfo<Map> info = new PageInfo<>();
        info.setCount(0L);
        List<Map> list = pandianDao.getAllRukuDetailByRukuId(pdid);
        if (list.size() > 0) {
            info.setData(list);
        }
        return info;
    }
}
